<?php

namespace common\models\kaoguobao;

use Yii;
use common\models\BaseModels;
use yii\behaviors\TimestampBehavior;

class WechatUser extends BaseModels
{

    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return '{{%wechat_user}}';
    }

    /**
     * 切换数据库(测试环境)
     */
    public static function getDb(){
        return Yii::$app->get('db_kgb');
    }

    /**
     * 给数据库的处理进行时间的操作
     */
    public function behaviors() {
        return [
            TimestampBehavior::className(),
        ];
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [[  'id',
                'openid',
                'nickname',
                'sex',
                'headimgurl',
                'province',
                'city',
                'access_token',
                'refresh_token',
                'created_at',
                'updated_at',
            ], 'safe'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id'            => Yii::t('app_table', 'ID'),
            'openid'        => Yii::t('app_table', 'openid'),
            'nickname'      => Yii::t('app_table', '微信昵称'),
            'sex'           => Yii::t('app_table', '性别'),
            'headimgurl'    => Yii::t('app_table', '微信头像'),
            'province'      => Yii::t('app_table', '国家'),
            'city'          => Yii::t('app_table', '城市'),
            'access_token'  => Yii::t('app_table', 'access_token'),
            'refresh_token' => Yii::t('app_table', 'refresh_token'),
            'created_at'    => Yii::t('app_table', '创建时间'),
            'updated_at'    => Yii::t('app_table', '修改时间'),
        ];
    }

    /**
     * @inheritdoc
     */
    public static function findIdentity($id)
    {
        return static::findOne(['id' => $id]);
    }

    /**
     * @inheritdoc
     */
    public static function findIdentityByAccessToken($token)
    {
        return static::findOne(['access_token' => $token]);
    }

    /**
     * @inheritdoc
     */
    public static function findByOpenid($openId)
    {
        return static::findOne(['openid' => $openId]);
    }

    public function getId(){
        return $this->getId();
    }

    public function getOpenid(){
        return $this->getOpenid();
    }


}
